package dao.interceptor;

import util.DBUtil;
import util.TransationUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class TransationInterceptor extends AbstractInterceptor{
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		TransationUtil.beginTransation();
		try {
			//正常提交，拦截器拦截所有的请求，强制用事务方法提交
			invocation.invoke();
			TransationUtil.commit();
			return null;
		} catch (Exception e) {
			//接收action中抛出的异常，回滚，并抛出到框架处理
			e.printStackTrace();
			TransationUtil.rollback();
			throw e;
		}finally{
			DBUtil.close();
		}
	}
}
